﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices.ActiveDirectory;
using System.Windows.Forms;

namespace ADFunctions
{
    public class ADF
    {
             
        public static bool CheckAdmin(UserPrincipal uname)
        {
            //MessageBox.Show("Checking Group");
            string gName = "BHI_SOLV_FSADMINS";
            //string gName2 = "Technician Level 4";
            PrincipalContext pContext = new PrincipalContext(ContextType.Domain);
            GroupPrincipal gPrincipal = GroupPrincipal.FindByIdentity(pContext, gName);
            //GroupPrincipal gPrincipal2 = GroupPrincipal.FindByIdentity(pContext, gName2);
            if (uname.IsMemberOf(gPrincipal))
            {
                return true;
                
            }
            else
            {
                //if(uname.IsMemberOf(gPrincipal2))
                //{
                //    return true;
               // }
                //else
                //{
                    return false;
                //}
            };

        }

        //Authenticate Against AD
        public static bool Authenticate(string userName, string password, string domain)
        {
            
            bool authentic = false;
            try
            {
                DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain,
                    userName, password);
                object nativeObject = entry.NativeObject;
                authentic = true;
            }
            catch (DirectoryServicesCOMException) { }
            return authentic;
        }

        //Get LDAP Name of BHI-Master
        public static string FriendlyDomainToLdapDomain(string friendlyDomainName)
        {
            string ldapPath = null;
            try
            {
                DirectoryContext objContext = new DirectoryContext(
                    DirectoryContextType.Domain, friendlyDomainName);
                Domain objDomain = Domain.GetDomain(objContext);
                ldapPath = objDomain.Name;
            }
            catch (DirectoryServicesCOMException e)
            {
                ldapPath = e.Message.ToString();
            }
            return ldapPath;
        }
        
        
        public static SearchResultCollection GetInformation(string cname, string domain, string uname, string password)
        {
            
            DirectoryEntry de = new DirectoryEntry("LDAP://" + domain , uname, password);

            try
            {
                DirectorySearcher ds = new DirectorySearcher(de);
                ds.Filter = "(CN=" + cname + ")";
                ds.PropertiesToLoad.Add("memberOf");

                SearchResultCollection dsFind = ds.FindAll();
                return dsFind; 
             }
            catch (Exception ex)
            {
                string debug = ex.Message;
                return null;
            }   
           
        }

        
        public static ArrayList EnumerateOU(string OuDn)
        {
            ArrayList alObjects = new ArrayList();
            try
            {
                DirectoryEntry directoryObject = new DirectoryEntry("LDAP://" + OuDn);
                foreach (DirectoryEntry child in directoryObject.Children)
                {
                    string childPath = child.Path.ToString();
                    alObjects.Add(childPath.Remove(0, 7));
                    //remove the LDAP prefix from the path

                    child.Close();
                    child.Dispose();
                }
                directoryObject.Close();
                directoryObject.Dispose();
            }
            catch (DirectoryServicesCOMException e)
            {
                MessageBox.Show("An Error Occurred: " + e.Message.ToString());
            }
            return alObjects;
        }

        public static void entitleSoftware(string sname, string cname)
        {
           
        }

        public static string AddUserToGroup(string userId, string groupName, string uname, string pass)
        {
            
            try
            {
                using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "BHI-MASTER",null,uname, pass))
                {
                    
                    GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, groupName);
                    group.Members.Add(pc, IdentityType.Name, userId);
                    MessageBox.Show(userId);
                    group.Save();
                    return "Entitlement Complete";
                }
            }
            catch (System.DirectoryServices.DirectoryServicesCOMException E)
            {
                    return E.ToString();

            }
        } 
    }
}
